import { kernel } from '../../../ts/base'
import { XWorkTask } from '../../../ts/base/schema'
import { TaskTypeName } from '../../../ts/core/work/task'
import orgCtrl from '../../../ts/controller'
import { TaskStatus } from '../../../ts/core/public'

const TaskCollName = 'work-task'

class CustomWork {
  todos: XWorkTask[] = []
  // getBelong(task: XWorkTask) {
  //   for (const company of orgCtrl.user.companys) {
  //     if (company.id === task.belongId) {
  //       return company
  //     }
  //   }
  //   return orgCtrl.user
  // }

  async loadTodos(): Promise<XWorkTask[]> {
    const todos: XWorkTask[] = []
    const res = await kernel.queryApproveTask({ id: '0' })
    if (res.success) {
      if (JSON.stringify(res.data) === '{}') {
        return []
      } else {
        res.data.result.forEach(item => {
          const groupTags = [
            // this.getBelong(item).name,
            item.taskType,
            item.approveType
          ]
          todos.push({ ...item, groupTags: groupTags } as XWorkTask)
        })
      }
    }

    return todos
  }

  async loadTasks(
    typeName: TaskTypeName,
    skip: number = 0
  ): Promise<XWorkTask[]> {
    const tasks: XWorkTask[] = []
    const result = await kernel.collectionLoad<XWorkTask[]>(
      orgCtrl.user.id,
      [],
      TaskCollName,
      {
        options: {
          match: this._typeMatch(typeName),
          sort: {
            createTime: -1
          }
        },
        skip: skip,
        take: 30
      }
    )
    if (result.success && result.data && result.data.length > 0) {
      result.data.forEach(item => {
        if (tasks.every(i => i.id !== item.id)) {
          const groupTags = [
            // this.getBelong(item).name,
            item.taskType,
            item.approveType
          ]
          tasks.push({ ...item, groupTags: groupTags } as XWorkTask)
        }
      })
    }
    return tasks.filter(i => this.isTaskType(i, typeName))
  }
  //某个具体办事
  async loadInfoTask(typeName: TaskTypeName,taskId: string): Promise<XWorkTask | undefined> {
    const data = await this.loadTasks(typeName, 0)
    return data.find(i => i.id === taskId)
  }
  private _typeMatch(typeName: TaskTypeName): any {
    switch (typeName) {
      case '已办':
        return {
          status: {
            _gte_: 100
          },
          records: {
            _exists_: true
          }
        }
      case '已发起':
        return {
          createUser: orgCtrl.user.id,
          nodeId: {
            _exists_: false
          }
        }
      case '已完结':
        return {
          createUser: orgCtrl.user.id,
          status: {
            _gte_: 100
          },
          nodeId: {
            _exists_: false
          }
        }
      case '抄送':
        return {
          approveType: '抄送'
        }
      default:
        return {
          status: {
            _lt_: 100
          }
        }
    }
  }

  isTaskType(task: XWorkTask, type: TaskTypeName){
    switch (type) {
      case '已办':
        return task.status >= TaskStatus.ApprovalStart
      case '已发起':
        return task.createUser === orgCtrl.user.id
      case '待办':
        return task.status < TaskStatus.ApprovalStart
      case '抄送':
        return task.approveType === '抄送'
      case '已完结':
        return task.createUser == orgCtrl.user.id
    }
  }

  clear() {
    this.todos = []
  }
}

export default new CustomWork()
